#/usr/bin/env python

######################
###author:McKelvin
### page: http://3c.etao.com/act_crazy12th.php
######################
import os,sys,time,httplib,json,random,datetime,platform

__platform = platform.system()
__signal = True;
if __platform == "Windows":
	import win32clipboard as wincb
	import win32con





tim1 = datetime.datetime.now()

######################
# config
# price = document.getElementById("span`id`").innerHTML = <--:)
######################
# 12/13 3rd
#id = "3";
#price_list=["1438.00"]

#12/13 4th
id = "4"
price_list=["4359.00"]

sleep_second=0.1;
log_file = "xetao.log"
fake_ip_prefix = "172.30."

def log(msg,p=True):
	msg = '[' + str(datetime.datetime.now())+'] '+msg
	f=open(log_file,'a');
	f.write(msg+'\n');
	f.close()
	if p:
		print msg

log('@START {id='+id+' , price=' + str(price_list)+'}')

#get wrong msg:json={"que":"2-1=","hit":"0"}
wrt = '''{"que":"2-1=","hit":"0"}'''
conn = httplib.HTTPConnection("fengkuang.etao.com")
conn.request("GET", "/?id="+id+"&p=-1")
r = conn.getresponse()
wrt = r.read()
r.close()
conn.close()



#crack
while True:
	done = False
	for price in price_list:
		try:
			log('    new Try')
			etao_headers = {
			"Host":"fengkuang.etao.com"
			,"User-Agent":"Mozilla/5.0 (Windows NT 6.1; rv:8.0.1) Gecko/20100101 Firefox/8.0.1"
			,"Referer":"http://3c.etao.com/act_crazy12th.php"
			,"Content-Type":"text/html; charset=GBK"
			,"X-Forwarded-For":fake_ip_prefix+str(random.randint(1,254)) +"."+ str(random.randint(1,254))
			##########################################
			# The sleep_second on line 26 is very short,without this Http Header 
			# Field[*X-Forwarded-For*],the IP *may be* detected as abusive accesses
			# by ETao Web Server.For Detail about *X-Forwarded-For*,Google,Wiki 
			# or visit my blog article:
			#
			# Reference:
			#  * http://blog.mckelv.in/?p=1141
			#  * http://en.wikipedia.org/wiki/X-Forwarded-For
			#  * http://tools.ietf.org/html/draft-petersson-forwarded-for
			##########################################
			}
			log('        Try IP:'+etao_headers['X-Forwarded-For'])
			conn.request("GET", "/?id="+id+"&p="+price,"",etao_headers)

			r = conn.getresponse()
			rt = r.read()
			if rt != wrt:
				rt = json.loads(rt[5:])
				win_que = rt['que']
				win_price = price;
				win_hit = rt['hit']
				done = True
				break
		except Exception,data:
			done = False
			print 'exception'
		finally:
			r.close()
			conn.close()
	if done:
		break
	else:
		time.sleep(sleep_second)
win_key=str(eval(win_que[:-1]))

log('    end Try')
#print crack info
#STYLE1
#'''[{000214A0-0000-0000-C000-000000000016}]
#[InternetShortcut]
#URL=
#s='''javascript:void((function(){KISSY.getScript('http://fengkuang.etao.com/?id='''+id+'''&p='''+win_price+'''',function(){KISSY.getScript('http://fengkuang.etao.com/?id='''+id+'''&p='''+win_price+'''&key='''+win_key+'''&que='''+win_que+'''',function(){if(json.win!=0)alert('json.win='+json.win)});});})());'''

#s = '''javascript:void((function(){function msg(num){dom=KISSY.DOM;msgs=KISSY.query("#zp_msg div");var h=document.body.offsetHeight;dom.get("#zp_mask2").style.height=h+"px";dom.get("#zp_mask2").style.display="block";msgs[num].style.display="block";var div=msgs[num].getElementsByTagName("area");for(var i=0;i<div.length;i++){(function(i){div[i].onclick=function(){msgs[num].style.display="none";dom.get("#zp_mask2").style.display="none";if((num==0)&&(i==0)){dom.get("#hide_ty").style.display="none";dom.get("#zp_mask").style.display="none";}returnfalse;}})(i)}}KISSY.getScript('http://fengkuang.etao.com/?id='''+id+'''&p='''+win_price+'''',function(){KISSY.getScript('http://fengkuang.etao.com/?id='''+id+'''&p='''+win_price+'''&key='''+win_key+'''&que='''+win_que+'''',function(){if(json.win!=0)msg(3);else msg(2);});});})());'''

#id = str(eval(id+'-1'))
#s = '''javascript:void((function(){KISSY.query("#zp_wrap input")['''+id+'''].value='''+win_price+'''; KISSY.query("#zp_wrap .ms")['''+id+'''].click();KISSY.later(function(){KISSY.query("#zp_yzm input")[0].value='''+win_key+'''; KISSY.DOM.get("#yz_btn").onclick();},100)})()); '''


#STYLE_LATEST
# left work done by preloading
# javascript:void((function(){var e=document.createElement('script');e.setAttribute('type','text/javascript');e.setAttribute('charset','UTF-8');e.setAttribute('src','https://kelvin-mirex-svn.googlecode.com/svn/trunk/misc/xt.js?' + new Date().getTime());document.getElementById('zp_wrap').appendChild(e)})());

id = str(eval(id+'-1'))
s = '''javascript:void((function(){KISSY.query("#zp_wrap input")['''+id+'''].value="'''+win_price+'''"; KISSY.query("#zp_wrap .ms")['''+id+'''].click();})());'''

if __platform == "Windows":
	wincb.OpenClipboard()
	wincb.EmptyClipboard()
	wincb.SetClipboardData(win32con.CF_TEXT, s) 
	log('YZM:'+win_que + '='+ win_key)
	log('ClipBoard:'+ wincb.GetClipboardData(win32con.CF_TEXT))
	wincb.CloseClipboard()
elif __platform == "Linux": #not tested yet
	os.popen('xsel', 'wb').write(s)
tim2 = datetime.datetime.now()
log('@FINISH in '+str(tim2 - tim1)+'\n\n')
